/*************************************************************
 * Title: HibernateBaseServiceImpl.java
 * Description: 
 * Author: Huang Shaobin
 * Email: huangshaobin@qtt.cn
 * CreateTime: Feb 11, 2013 3:55:13 PM
 * Copyright © 北京全天通信息咨询服务有限公司 All right reserved
 ************************************************************/
package snow.biz.base.service.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.InitializingBean;

import snow.biz.base.service.IBaseService;
import snow.biz.system.model.User;
import snow.core.context.AccessContext;
import snow.core.persistence.Pagination;
import snow.core.persistence.hibernate.HibernatePersistenceSupport;
import snow.core.persistence.query.Conditions;
import snow.core.persistence.query.Order;

/**
 * 使用Hibernate实现的底层
 */
public abstract class HibernateBaseServiceImpl extends HibernatePersistenceSupport implements IBaseService, InitializingBean {

	public void delete(Object entity) {
		this.delete(entity, null);
	}

	public void delete(Object entity, LockMode lockMode) {
		super.delete(entity, lockMode);
	}

	public <T> List<T> find(Class<T> entityClass, Conditions conditions, List<Order> orders, int start, int limit) {
		return this.findByCriteria(toDetachedCriteriaFrom(conditions, orders, entityClass), entityClass, start, limit);
	}

	public <T> T find(Class<T> entityClass, Serializable id) {
		return this.get(entityClass, id);
	}

	public <T> List<T> findByCriteria(DetachedCriteria detachedCriteria, Class<T> entityClass, int start, int limit) {
		return super.findByCriteria(detachedCriteria, entityClass, start, limit);
	}

	public Pagination findByCriteriaForPaging(DetachedCriteria detachedCriteria, int start, int limit) {
		return super.findByCriteriaForPaging(detachedCriteria, start, limit);
	}

	public <T> T findByCriteriaForUnique(DetachedCriteria detachedCriteria, Class<T> entityClass) {
		return super.findByCriteriaForUnique(detachedCriteria, entityClass);
	}

	public Pagination findForPaging(Class<?> entityClass, Conditions conditions, List<Order> orders, int start, int limit) {
		return this.findByCriteriaForPaging(toDetachedCriteriaFrom(conditions, orders, entityClass), start, limit);
	}

	public <T> T findForUnique(Conditions conditions, List<Order> orders, Class<T> entityClass) {
		return findByCriteriaForUnique(toDetachedCriteriaFrom(conditions, orders, entityClass), entityClass);
	}

	public <T> T get(Class<T> entityClass, Serializable id) {
		return this.get(entityClass, id, null);
	}

	public <T> T get(Class<T> entityClass, Serializable id, LockMode lockMode) {
		return super.get(entityClass, id, lockMode);
	}

	public User getCurrentUser() {
		AccessContext context = AccessContext.get();
		Integer currentUserId = context.get("session_current_user_id", Integer.class);
		return currentUserId == null ? null : super.get(User.class, currentUserId);
	}

	public <T> T load(Class<T> entityClass, Serializable id) {
		return this.load(entityClass, id, null);
	}

	public <T> T load(Class<T> entityClass, Serializable id, LockMode lockMode) {
		return super.load(entityClass, id, lockMode);
	}

	public <ID extends Serializable> ID save(Object entity, Class<ID> idClass) {
		return this.save(entity, idClass, null);
	}

	public <ID extends Serializable> ID save(Object entity, Class<ID> idClass, LockMode lockMode) {
		return super.save(entity, idClass, lockMode);
	}

	public void update(Object entity) {
		this.update(entity, null);
	}

	public void update(Object entity, LockMode lockMode) {
		super.update(entity, lockMode);
	}

}
